﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cocon90.Lib.Util
{

    /// <summary>
    /// 用来延时执行某事
    /// </summary>
    public class DelayInvoker
    {
        /// <summary>
        /// 创建一个新实例
        /// </summary>
        /// <param name="interval"></param>
        /// <returns></returns>
        public static DelayInvoker CreateInstence(int interval)
        {
            return new DelayInvoker(interval);
        }
        public int Interval { get; set; }
        /// <summary>
        /// 定义多少毫秒的延时
        /// </summary>
        /// <param name="interval"></param>
        DelayInvoker(int interval)
        {
            this.Interval = interval;
        }
        /// <summary>
        /// Interval毫秒后开始执行某事
        /// </summary>
        /// <param name="action"></param>
        public void Invoke(Action action)
        {
            System.Timers.Timer th = new System.Timers.Timer(this.Interval);
            th.Elapsed += (s, e) =>
            {
                th.Stop();
                action();
            };
            th.Start();
        }

    }
}
